//
//  TopicCollectionCell.m
//  Zaker
//
//  Created by dllo on 16/3/9.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "TopicCollectionCell.h"

@interface TopicCollectionCell ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *dataArr;
@property (nonatomic, assign) NSInteger number;
@property (nonatomic, assign) BOOL isUpLoading;//用来标记是上拉还是下拉

@end

@implementation TopicCollectionCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createTableView];
        
        self.dataArr = [NSMutableArray array];
    }
    return self;
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    [super applyLayoutAttributes:layoutAttributes];
    [self dataHandler];
    
    //话题页下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(dataHandler)];
    [_tableView.mj_header beginRefreshing];
    
    [self addFooterRefresh];
}

//上拉加载
- (void)addFooterRefresh {
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        self.isUpLoading = YES;//标记为上拉
        self.number += 10;
        //重新加载数据
        [self loadnewdata:self.number];
    }];
}

//话题页上拉加载
- (void)loadnewdata:(NSInteger)page
{
    NSString *str = [NSString stringWithFormat:@"http://c.m.163.com/newstopic/list/expert/%ld-10.html", page] ;
    [LHAFNetworkTool getUrl:str body:nil response:LHJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
        NSDictionary *dic = resposeObject[@"data"];
        NSArray *arr = dic[@"expertList"];
        NSArray *arrTemp = [NSArray array];
        arrTemp = [TopicModel modelHandlerWithArray:arr];
        //arrTemp = [Topic modelHandlerWithArray:arr];
        if (arrTemp.count != 0) {
            for (NSInteger i = 0; i < arrTemp.count; i++) {
                [self.dataArr addObject:arrTemp[i]];
            }
            [_tableView reloadData];
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    //self.number += 10;
    [_tableView.mj_footer endRefreshing];
}

//话题页的数据处理
-(void)dataHandler
{
    NSString *str = @"http://c.m.163.com/newstopic/list/expert/0-10.html";
    [LHAFNetworkTool getUrl:str body:nil response:LHJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
        //  专家数组+
        NSDictionary *dic = resposeObject[@"data"];
        NSArray *arr = dic[@"expertList"];
        self.dataArr = [NSMutableArray array];
        self.dataArr = [TopicModel modelHandlerWithArray:arr];
        //self.dataArr = [Topic modelHandlerWithArray:arr];
        [_tableView reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    [_tableView.mj_header endRefreshing];
}

- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:self.contentView.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = 0;
    [self.contentView addSubview:_tableView];
    [_tableView registerClass:[TopicTableViewCell class] forCellReuseIdentifier:@"cell"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 300;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TopicModel *model = _dataArr[indexPath.row];
    //Topic *model = _dataArr[indexPath.row];
    TopicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = 0;
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TopicModel *model = _dataArr[indexPath.row];
    //Topic *model = _dataArr[indexPath.row];
    [self.delegate topicPassWithModel:model];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
